<?php
class ControllerSaleTransactions extends Controller {
	private $error = array();

	public function index() {
	$this->language->load('sale/customer');
		$this->data['token'] = $this->session->data['token'];
		$this->document->setTitle('Thanh toán cho thành viên');
		$this->template = 'sale/transactions.tpl';	
		$this->children = array(
			'common/footer',
			'common/header'	
		);
		$this->response->setOutput($this->render());	
	}
	public function getTransactionInfo() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
		if($results){
			$data = array(
				'customer_id'       => $this->request->get['customer_id'],
				'filter_date_start' => $this->request->get['filter_date_start'],
				'filter_date_end'   => $this->request->get['filter_date_end'],
				'status'            => 'true'
			);
			$this->data['name'] = $results['name'];
			$this->data['madn'] = $results['madn'];
			// hoa hồng trực tiếp
			$results1 = $this->model_sale_customer->getHHTrucTiep($data);	
			// hoa hồng gián tiếp
			$results2 = $this->model_sale_customer->getHHGianTiep($data);
			
			$this->data['hhtuvan'] = array();
			$this->data['hhbaotro'] = array();
			
			$this->data['totalbaotro'] = 0;
			
			$this->data['totaltuvan'] = 0;
			
			$this->data['total'] = 0;
			// hoa hồng trực tiếp
			
			if($results1){
				foreach($results1 as $result){				
				  $cus = $this->model_sale_customer->getCustomer($result['customer_id']);
					if($cus){
						$this->data['totalbaotro'] += $result['price'];
						$this->data['hhbaotro'][] = array(
							'date_added' => date('d/m/Y',strtotime($result['date_added'])),
							'name' => $cus['name'],
							'madn' => $cus['madn'],
							'telephone' => $cus['telephone'],
							'email' => $cus['email'],
							'hoahong' => $this->currency->format($result['price'])
						);	
				}}
			}	
			
			// hoa hồng gián tiếp
			if($results2){
				foreach($results2 as $result){
				$cus = $this->model_sale_customer->getCustomer($result['customer_id']);
					if($cus){
						$this->data['totaltuvan'] += $result['price'];
						$this->data['hhtuvan'][] = array(
							'date_added' => date('d/m/Y',strtotime($result['date_added'])),
							'name' => $cus['name'],
							'madn' => $cus['madn'],
							'email' => $cus['email'],
							'telephone' => $cus['telephone'],
							'hoahong' => $this->currency->format($result['price'])
						);
					}
				}
			}
			$this->data['total'] = $this->currency->format($this->data['totaltuvan'] + $this->data['totalbaotro']);
			$this->data['total1'] = $this->data['totaltuvan'] + $this->data['totalbaotro'];
			$this->data['totaltuvan'] = $this->currency->format($this->data['totaltuvan']);
			$this->data['totalbaotro'] = $this->currency->format($this->data['totalbaotro']);
		}
		
		$this->template = 'sale/form_transaction.tpl';

		$this->response->setOutput($this->render());
	}
	
	public function activeTransaction() {
		$this->language->load('sale/customer');
		$this->load->model('sale/customer');
		$results = $this->model_sale_customer->getCustomer($this->request->get['customer_id']);
		if($results){
			$data = array(
				'customer_id'       => $this->request->get['customer_id'],
				'filter_date_start' => $this->request->get['filter_date_start'],
				'filter_date_end'   => $this->request->get['filter_date_end']
			);
			// hoa hồng trực tiếp
			$this->model_sale_customer->transactionsHHTrucTiep($data);	
			// hoa hồng gián tiếp
			$this->model_sale_customer->transactionsHHGianTiep($data);
			
			$this->model_sale_customer->addTransactionsCustomer($this->request->get['customer_id'], $this->request->get['total'], $this->request->get['comment']);
			
			$this->session->data['success'] = 'Hệ thống thanh toán thành công ' . $this->currency->format($this->request->get['total']) . ' cho thành viên ' . $results['madn'];
		} else {
			$this->session->data['warning'] = 'Vui long kiểm tra lại, thành viên không tồn tại trong hệ thống';
		}
		$this->redirect($this->url->link('sale/customer', 'token=' . $this->session->data['token'], 'SSL'));
	}
}
?>